package cm.example.shoppingplatformspringboot.mapper;


import cm.example.shoppingplatformspringboot.entity.Goods;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface GoodsMapper {
    //分页查询商品列表
    @Select("SELECT * FROM wn_goods WHERE status = 'Y' ORDER BY id DESC LIMIT #{start}, #{size}")
    List<Goods> selectAll(int start, int size);

    //查询商品总数
    @Select("SELECT count(*)  FROM wn_goods WHERE status = 'Y'")
    int countAll();

    //获取热销商品
    @Select("select * from wn_goods where hottest = 'Y'")
    List<Goods> hottestGoods();

    // 获取所有商品分类
    @Select("SELECT id, name FROM wn_category")
    List<Map<String, Object>> getAllShopGenres();

    // 根据商品分类查询商品列表
    @Select("select g.*, c.name AS category_name from wn_category c " +
            "LEFT JOIN wn_goods g ON c.id = g.category_id " +
            "where c.name = #{value} and g.status = 'Y' " +
            "ORDER BY g.id DESC " +
            "LIMIT #{start}, #{size}")
    List<Goods> getGoodsListByGenre(String value, int start, int size);

    // 根据商品分类查询商品总数
    @Select("SELECT COUNT(*) FROM wn_goods g " +
            "LEFT JOIN wn_category c ON g.category_id = c.id " +
            "WHERE c.name = #{value} and g.status = 'Y'")
    int countGoodsByGenre(String value);

    // 根据商品名称模糊查询商品列表
    @Select("select * from wn_goods " +
            "where name LIKE CONCAT('%', #{input}, '%') and status = 'Y' " +
            "ORDER BY id DESC " +
            "LIMIT #{start}, #{size}")
    List<Goods> searchGoods(String input, int start, int size);

    // 根据商品名称模糊查询商品总数
    @Select("SELECT COUNT(*) FROM wn_goods WHERE name LIKE CONCAT('%', #{name}, '%') and status = 'Y'")
    int countGoodsByName(String name);

    // 根据商品名称模糊查询商品名称
    @Select("select name from wn_goods where name LIKE CONCAT('%', #{name}, '%') ")
    List<String> searchGoodsByName(String name);

    // 根据商品id查看商品
    @Select("select * from wn_goods where id = #{id}")
    Goods selectById(Integer id);

    // 更新商品库存
    @Update("UPDATE wn_goods SET stock = stock + #{quantity} WHERE id = #{goodsId}")
    void updateStock(@Param("goodsId") Integer goodsId, @Param("quantity") Integer quantity);
}
